package com.hkemmm.shop.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hkemmm.shop.entity.*;
import com.hkemmm.shop.service.IAddressService;
import com.hkemmm.shop.service.IAreaService;
import com.hkemmm.shop.service.ICityService;
import com.hkemmm.shop.service.IProvinceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/address")
public class AddressController {

    @Autowired
    private IAddressService addressService;
    @Autowired
    private IProvinceService provinceService;
    @Autowired
    private ICityService cityService;
    @Autowired
    private IAreaService areaService;

    @RequestMapping("/provinces")
    @ResponseBody
    public List<Provinces> getAllProvinces() {
        List<Provinces> provincesList = new ArrayList<>();
        provincesList = addressService.getAllProvinces();

        return provincesList;
    }

    @RequestMapping("/cities")
    @ResponseBody
    public List<Cities> getAllCities(String pid) {
        List<Cities> citiesList = new ArrayList<>();
        citiesList = addressService.getAllCities(pid);

        return citiesList;
    }

    @RequestMapping("/areas")
    @ResponseBody
    public List<Areas> getAllAreas(String cid) {
        List<Areas> areasList = new ArrayList<>();
        areasList = addressService.getAllAreas(cid);

        return areasList;
    }


    @RequestMapping("/getOneAddress")
    @ResponseBody
    public Map getAddress(Integer id) {
        Map<String, Object> map = new HashMap<>();
        Addreas address = addressService.selectByPrimaryKey(id);

        map.put("address", address);
        map.put("province", provinceService.selectByPrimaryKey(address.getPid()));
        map.put("city", cityService.selectByPrimaryKey(address.getCid()));
        map.put("area", areaService.selectByPrimaryKey(address.getAid()));
        return map;
    }


    @RequestMapping("/delAddress")
    @ResponseBody
    public String delAdddress(Integer id) {
        int i = addressService.deleteByPrimaryKey(id);
        System.out.println(i);
        if (i > 0) {
            return "ok";
        }
        return "err";
    }

    @RequestMapping("/addressInfo")
    public ModelAndView AddressInfo(Integer uid, ModelAndView mav) {
        List<AddressInfo> addressInfoList = addressService.getAddressInfo(uid);
        mav.addObject("address", addressInfoList);
        mav.setViewName("/WEB-INF/pages/address");

        return mav;
    }


    @RequestMapping("/record")
    @ResponseBody
    public String addAndUpdate(Addreas addres, String provinceid, String cityid, String areaid) {
        int i = 0;


        addres.setPid(provinceService.selectProvince(provinceid).getId());
        addres.setCid(cityService.selectCity(cityid).getId());
        addres.setAid(areaService.selectArea(areaid).getId());

//        System.out.println("111111----"+addres);


        if (addres.getId() == null) {
            addressService.insertSelective(addres);
        } else {
            addressService.updateByPrimaryKeySelective(addres);
        }

        return "ok";
    }

    @RequestMapping("/getAddressInfo")
    @ResponseBody
    public List<AddressInfo> getAddressInfo(Integer uid) {
        List<AddressInfo> addressInfoList = addressService.getAddressInfo(uid);
//        System.out.println(addressInfoList.size());
        if (addressInfoList.size() == 0) {
            return null;
        }

        return addressInfoList;
    }

    @RequestMapping("/setDefault")
    @ResponseBody
    public String setDefaultAddres(Integer uid, Integer id) {
        addressService.setAllAddresDefault(uid);
        addressService.setDefault(id);

        return "ok";
    }

    @RequestMapping("/addresNum")
    @ResponseBody
    public Integer addresNum(Integer uid) {
        return addressService.addresNum(uid);
    }
}
